<template>
  <div class="coupon-history">
    <el-card>
      <div slot="header" class="clearfix">
        <span>优惠券领取记录</span>
        <div style="float: right;">
          <el-tag type="info">总记录: {{ pagination.total }}</el-tag>
          <el-tag type="success" style="margin-left: 10px;">今日领取: {{ todayCount }}</el-tag>
        </div>
      </div>

      <!-- 搜索栏 -->
      <div class="search-bar">
        <el-form :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item label="时间范围">
            <el-date-picker
              v-model="searchForm.order_time"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              @change="handleDateChange"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="关键词搜索">
            <el-input 
              v-model="searchForm.search" 
              placeholder="请输入用户昵称、手机号或备注" 
              clearable
              style="width: 300px;"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch" icon="el-icon-search">搜索</el-button>
            <el-button @click="resetSearch" icon="el-icon-refresh">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 优惠券记录列表 -->
      <el-table :data="recordList" v-loading="loading" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="nickname" label="用户昵称" width="120"></el-table-column>
        <el-table-column prop="mobile" label="手机号" width="130"></el-table-column>
        <el-table-column prop="remark" label="备注" width="100"></el-table-column>
        <el-table-column prop="title" label="优惠券名称" min-width="150"></el-table-column>
        <el-table-column prop="money" label="售价" width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.money === '0.00'">免费</span>
            <span v-else>¥{{ scope.row.money }}</span>
          </template>
        </el-table-column>
        <el-table-column label="使用状态" width="100">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'info'">
              {{ scope.row.status === 1 ? '已使用' : '未使用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否过期" width="100">
          <template slot-scope="scope">
            <el-tag :type="scope.row.if_expire === 1 ? 'danger' : 'success'">
              {{ scope.row.if_expire === 2 ? '已过期' : '未过期' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="领取时间" width="160"></el-table-column>
        <el-table-column prop="exp_time" label="过期时间" width="160"></el-table-column>
       
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pagination.currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pagination.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total">
        </el-pagination>
      </div>
    </el-card>

    <!-- 详情弹窗 -->
    <el-dialog
      title="优惠券记录详情"
      :visible.sync="detailDialogVisible"
      width="600px"
    >
      <el-descriptions :column="2" border>
        <el-descriptions-item label="记录ID">{{ currentRecord.id }}</el-descriptions-item>
        <el-descriptions-item label="用户ID">{{ currentRecord.uid }}</el-descriptions-item>
        <el-descriptions-item label="用户昵称">{{ currentRecord.nickname }}</el-descriptions-item>
        <el-descriptions-item label="手机号">{{ currentRecord.mobile }}</el-descriptions-item>
        <el-descriptions-item label="备注">{{ currentRecord.remark || '-' }}</el-descriptions-item>
        <el-descriptions-item label="优惠券ID">{{ currentRecord.coupon_id }}</el-descriptions-item>
        <el-descriptions-item label="优惠券名称">{{ currentRecord.title }}</el-descriptions-item>
        <el-descriptions-item label="售价">
          <span v-if="currentRecord.money === '0.00'">免费</span>
          <span v-else>¥{{ currentRecord.money }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="使用状态">
          <el-tag :type="currentRecord.status === 1 ? 'success' : 'info'">
            {{ currentRecord.status === 1 ? '已使用' : '未使用' }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="是否过期">
          <el-tag :type="currentRecord.if_expire === 1 ? 'danger' : 'success'">
            {{ currentRecord.if_expire === 1 ? '已过期' : '未过期' }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="领取时间">{{ currentRecord.create_time }}</el-descriptions-item>
        <el-descriptions-item label="更新时间">{{ currentRecord.update_time }}</el-descriptions-item>
        <el-descriptions-item label="过期时间">{{ currentRecord.exp_time }}</el-descriptions-item>
      </el-descriptions>

      <div slot="footer" class="dialog-footer">
        <el-button @click="detailDialogVisible = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { fetchCouponRecordList } from '@/api/shopping'

export default {
  name: 'DiscountCouponListHistory',
  data() {
    return {
      loading: false,
      recordList: [],
      todayCount: 0,
      searchForm: {
        order_time: '',
        search: ''
      },
      pagination: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      // 详情弹窗
      detailDialogVisible: false,
      currentRecord: {}
    }
  },
  created() {
    this.getRecordList()
  },
  methods: {
    // 获取优惠券记录列表
    async getRecordList() {
      this.loading = true
      try {
        const params = {
          page: this.pagination.currentPage.toString(),
          page_size: this.pagination.pageSize.toString(),
          order_time: this.searchForm.order_time || '',
          search: this.searchForm.search || ''
        }
        const response = await fetchCouponRecordList(params)
        if (response.code === 1) {
          this.recordList = response.data.data || []
          this.pagination.total = response.data.total || 0
          this.todayCount = response.data.today_count || 0
        } else {
          this.$message.error(response.msg || '获取优惠券记录失败')
        }
      } catch (error) {
        this.$message.error('获取优惠券记录失败')
        console.error('获取优惠券记录失败:', error)
      } finally {
        this.loading = false
      }
    },

    // 搜索
    handleSearch() {
      this.pagination.currentPage = 1
      this.getRecordList()
    },

    // 重置搜索
    resetSearch() {
      this.searchForm = {
        order_time: '',
        search: ''
      }
      this.pagination.currentPage = 1
      this.getRecordList()
    },

    // 日期范围改变处理
    handleDateChange(value) {
      if (value && value.length === 2) {
        this.searchForm.order_time = `${value[0]}_${value[1]}`
      } else {
        this.searchForm.order_time = ''
      }
    },

    // 分页大小改变
    handleSizeChange(val) {
      this.pagination.pageSize = val
      this.pagination.currentPage = 1
      this.getRecordList()
    },

    // 当前页改变
    handleCurrentChange(val) {
      this.pagination.currentPage = val
      this.getRecordList()
    },

    // 查看详情
    handleView(row) {
      this.currentRecord = { ...row }
      this.detailDialogVisible = true
    }
  }
}
</script>

<style scoped>
.coupon-history {
  padding: 20px;
}

.search-bar {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}

.dialog-footer {
  text-align: right;
}

.el-table {
  margin-top: 20px;
}

.el-form-item {
  margin-bottom: 20px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}
</style>
